package com.qnbc.mystery.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qnbc.mystery.core.annotation.Log;
import com.qnbc.mystery.core.controller.BaseController;
import com.qnbc.mystery.core.domain.AjaxResult;
import com.qnbc.mystery.core.enums.BusinessType;
import com.qnbc.mystery.core.page.TableDataInfo;
import com.qnbc.mystery.core.utils.poi.ExcelUtil;
import com.qnbc.mystery.pojo.domain.FoodOrder;
import com.qnbc.mystery.pojo.domain.User;
import com.qnbc.mystery.utils.StringUtil;
import com.qnbc.mystery.utils.TokenUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qnbc.mystery.service.IFoodOrderService;

/**
 * 订单
 *
 * @author guzj
 * @date 2024-01-24
 */
@RestController
@RequestMapping("/mystery/order")
public class FoodOrderController extends BaseController {
    @Autowired
    private IFoodOrderService foodOrderService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FoodOrder foodOrder) {
        startPage();
        User currentUser = TokenUtils.getCurrentUser();
        LambdaQueryWrapper<FoodOrder> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(0!= foodOrder.getStatus(), FoodOrder::getStatus, foodOrder.getStatus());
        queryWrapper.eq(StringUtil.isNotEmpty(foodOrder.getOrderNo()), FoodOrder::getOrderNo, foodOrder.getOrderNo());
        queryWrapper.eq(StringUtil.isNotEmpty(foodOrder.getOrderType()), FoodOrder::getOrderType, foodOrder.getOrderType());
        queryWrapper.eq(null != currentUser.getId(), FoodOrder::getCreatedBy, currentUser.getId()).orderByDesc(FoodOrder::getId);
        List<FoodOrder> list = foodOrderService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(foodOrderService.selectFoodOrderById(id));
    }

    /**
     * 修改订单
     */
    @Log(title = "修改订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FoodOrder foodOrder) {
        return toAjax(foodOrderService.updateFoodOrder(foodOrder));
    }

    /**
     * 删除订单
     */
    @Log(title = "删除订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(foodOrderService.deleteFoodOrderByIds(ids));
    }


    /**
     * 确认收货
     */
    @ApiOperation("确认收货")
    @GetMapping("/deliverGoods")
    public AjaxResult deliverGoods(FoodOrder order) {
        //订单更新为已发货, 并记录物流单号
        order.setStatus(5);
        return toAjax(foodOrderService.updateFoodOrder(order));
    }
}
